<template>
  <MainSlot v-loading="loading">
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" @search="search" @isUnfold="(v) => { unfoldFlag = v }">
      </SearchForm>
    </template>

    <template v-slot:tab>
      <EnumSlot @handleEvent="getEnumEvent" :selectIndex="selectIndex" :enumList="enumList"></EnumSlot>
    </template>

    <template v-slot:table>
      <TableData :config="table_config" :tableThead="tableThead" :dataList="dataList" :isSelect="false" :loading="loading" @selection-change="selectionChange">
        <template v-slot:checkStage="{ row }">
          {{ radioObj[row.checkStageId] }}
        </template>
      </TableData>

    </template>

    <template v-slot:pagination>
      <Pagination @current-change="handleCurrentChange" :page="page" :total="total">
      </Pagination>
    </template>

    <template v-slot:dialogModule>
      <AddEdit :title="'查看质检单'" @closeDialog="closeDialog" :addEditFlag="addEditFlag" :dialogVisible="dialogVisible" :data="formData">
      </AddEdit>
    </template>
  </MainSlot>
</template>
  
  <script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from "@/components-production-management/Pagination";
import SearchForm from "@/components-production-management/SearchForm";
import TableData from "@/components-production-management/TableData";
import BtnSlot from "@/components-production-management/BtnSlot";
import EnumSlot from "@/components-production-management/EnumSlot";
import AddEdit from "./components/addEdit.vue";
import inspectionManagement from "@/api/quality-management/inspectionManagement";
import inspectionPhase from "@/api/quality-management/inspectionPhase.js";
import varietyManagement from "@/api/basic-info-manage/fishVariety";
export default {
  components: {
    MainSlot,
    Pagination,
    SearchForm,
    TableData,
    BtnSlot,
    EnumSlot,
    AddEdit,
  },
  data() {
    return {
      unfoldFlag: false,
      loading: false,
      dialogVisible: false,
      addEditFlag: "add",
      selectList: [],
      formData: {},
      page: 1,
      selectIndex: 0,
      searchFormData: {
        status: "COMPLETION",
      },
      total: 10,
      enumList: [
        { type: "COMPLETION", label: "完成" },
        { type: "REJECTION", label: "拒绝" },
      ],
      btnList: ["export"],
      dataList: [],
      tableThead: [
        { isShow: true, label: "供应商", prop: "supplier", minWidth: "120px" },
        {
          isShow: true,
          label: "检测阶段",
          slotName: "checkStage",
          minWidth: "120px",
        },
        { isShow: true, label: "检验品种", prop: "plantVarietyName" },
        { isShow: true, label: "检验人", prop: "staffName" },
        { isShow: true, label: "检验时间", prop: "checkTime" },
      ],
      radioList: [],
      varietyList: [],
      radioObj: {},
      // 配置项
      table_config: {
        isCheckBox: true,
        isOperation: true,
        // 表格操作列
        operation: {
          label: "操作",
          width: "200",
          cols: [
            {
              label: "结果判定",
              type: "primary",
              flag: "edit",
              handleRow: this.handleRow,
            },
          ],
        },
      },
      formItemList: [
        {
          label: "检验阶段",
          type: "select",
          selectOptions: [],
          param: "checkStageId",
          defaultSelect: "", // 下拉框默认选中项
          multiple: false,
          isShow: true,
        },
        {
          label: "检验品种",
          type: "select",
          selectOptions: [],
          param: "plantVarietyId",
          defaultSelect: "", // 下拉框默认选中项
          multiple: false,
          isShow: true,
        },
      ],
    };
  },

  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      let res = await inspectionPhase.queryByCondition({});
      this.radioList = res.data || [];
      this.radioList.forEach((item) => {
        this.radioObj[item.checkStageId] = item.checkStageName;
      });
      //获取品种；
      let { data: varietyList } = await varietyManagement.queryByCondition({});
      this.varietyList = varietyList || [];
      this.queryPageByCondition();
      this.setFormOptions();
    },
    setFormOptions() {
      let list = JSON.parse(JSON.stringify(this.formItemList));
      this.formItemList = [];
      list.forEach((item, index) => {
        if (item.param.indexOf("checkStageId") !== -1) {
          console.log(this.facilityList);
          let selectOptions = this.radioList.map((data) => {
            return {
              text: data.checkStageName,
              value: data.checkStageId,
            };
          });
          item.defaultSelect = this.searchFormData[item.param];
          item.selectOptions = selectOptions;
        }
        if (item.param.indexOf("plantVarietyId") !== -1) {
          let selectOptions = this.varietyList.map((data) => {
            return {
              text: data.varietyName,
              value: data.fishVarietyId,
            };
          });
          item.defaultSelect = this.searchFormData[item.param];
          item.selectOptions = selectOptions;
        }
      });
      this.$nextTick(() => {
        this.$set(this, "formItemList", list);
        console.log(this.formItemList);
      });
    },
    //获取选中Enum
    getEnumEvent(enumInfo) {
      this.page = 1;
      this.selectIndex = enumInfo.index || 0;
      this.$set(this.searchFormData, "status", enumInfo.data.type);
      this.queryPageByCondition();
    },
    delList(deleteList = [], key = "checkScopeTypeId") {
      let list = [];
      deleteList.forEach((item) => {
        list.push(item[key]);
      });
      console.log(list);
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await inspectionManagement.delList(list);
          if (res.success) {
            this.$message.success(res.msg || "删除成功");
            this.queryPageByCondition();
          } else {
            this.$message.warning(res.msg || "删除失败");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: 10,
        entity: {
          ...this.searchFormData,
        },
      };
      inspectionManagement
        .queryPageByCondition(params)
        .then((res) => {
          if (res.success) {
            this.dataList = res.data.content || [];
            this.total = res.data.totalElements;
            if (res.data.totalPages && this.page > res.data.totalPages) {
              this.page = res.data.totalPages;
              this.queryPageByCondition();
            }
          } else {
            this.$message.warning(res.msg || "分页查询失败");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    search(data) {
      this.page = 1;
      this.searchFormData = { ...this.searchFormData, ...data };
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
    // 操作方法
    handleRow(rowData, item) {
      if (item.flag == "edit") {
        this.dialogVisible = true;
        this.formData = JSON.parse(JSON.stringify(rowData));
      } else if (item.flag == "del") {
        this.delList([rowData]);
      }
      this.addEditFlag = item.flag;
    },
    // 多选方法
    selectionChange(val) {
      this.selectList = val || [];
    },
    //获取选中Btn
    getBtnEvent(flag) {
      console.log(flag);
      if (flag == "add") {
        this.dialogVisible = true;
      } else if (flag == "delete") {
        if (this.selectList.length > 0) {
          this.delList(this.selectList);
        } else {
          this.$message.warning("请先选择表单数据项后再删除！");
        }
      }
      this.addEditFlag = flag;
    },
    //关闭弹窗
    closeDialog() {
      this.formData = {};
      this.dialogVisible = false;
      this.queryPageByCondition();
    },
  },
};
</script>
  
  <style lang="scss" scoped></style>